
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulebase.dto.DeptMailboxDto;
import com.rt.schedulebase.entity.DeptMailbox;
import com.rt.schedulebase.mapper.DeptMailboxMapper;
import com.rt.schedulenew.api.IDeptMailboxService;
import com.rt.schedulenew.utils.base.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptMailboxServiceImpl extends ServiceImpl<DeptMailboxMapper, DeptMailbox> implements IDeptMailboxService {
    @Override
    public boolean insertSelective(DeptMailbox deptMailbox) {
        return retBool(baseMapper.insert(deptMailbox));
    }

    @Override
    public boolean deleteByPrimaryKey(String key) {
        return retBool(baseMapper.deleteById(key));
    }

    @Override
    public boolean updateByPrimaryKeySelective(DeptMailbox deptMailbox) {
        return retBool(baseMapper.updateById(deptMailbox));
    }

    @Override
    public DeptMailbox selectByPrimaryKey(String key) {
        return baseMapper.selectById(key);
    }

    @Override
    public List<DeptMailbox> getList(DeptMailboxDto dto) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<DeptMailbox> qw = new QueryWrapper<>();
        map.put("READ_FLAG", dto.getReadFlag());
        map.put("PROCESS_FLAG", dto.getProcessFlag());
        map.put("USER_ID", dto.getUserId());
        qw.allEq(map, false);
        List<DeptMailbox> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<DeptMailbox> getListAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean delete(DeptMailbox deptMailbox) {
        Map<String, Object> map = new HashMap<>();
        UpdateWrapper<DeptMailbox> uw = new UpdateWrapper<>();
        map.put("NOTICE_ID", deptMailbox.getNoticeId());
        uw.allEq(map, false);
        return retBool(baseMapper.delete(uw));
    }

    @Override
    public boolean update(DeptMailbox deptMailbox) {
        Map<String, Object> map = new HashMap<>();
        UpdateWrapper<DeptMailbox> uw = new UpdateWrapper<>();
        if (StringUtils.isNotBlank(deptMailbox.getNoticeId())) {
            uw.in("NOTICE_ID", CommonUtil.split2List(deptMailbox.getNoticeId()));
        }
        map.put("USER_ID", deptMailbox.getUserId());
        uw.allEq(map, false);
        return retBool(baseMapper.update(deptMailbox, uw));
    }

    @Override
    public DeptMailbox getInfo(DeptMailboxDto dto) {
        Map<String, Object> map = new HashMap<String, Object>();
        QueryWrapper<DeptMailbox> qw = new QueryWrapper<>();
        map.put("NOTICE_ID", dto.getNoticeId());
        qw.allEq(map, false);
        return baseMapper.selectOne(qw);
    }

    @Override
    public int unreadCount(DeptMailbox deptMailbox) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<DeptMailbox> qw = new QueryWrapper<>();
        map.put("USER_ID", deptMailbox.getUserId());
        map.put("READ_FLAG", "0");
        map.put("PROCESS_FLAG", "0");
        qw.allEq(map, false);
        return ((DeptMailboxMapper) baseMapper).selectCount(qw);
    }
}
